package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.risetechnology.adapter.ShoppingListDetailsAdapter;
import com.risetechnology.common.Const;
import com.risetechnology.common.Shared;
import com.risetechnology.data.ShopIngredient;
import com.risetechnology.master.NavigationActivity;

public class ActivityShoppingDetails extends NavigationActivity {

	private ListView listviewIngredients;
	public  ArrayList<ShopIngredient> ingredientList;
	private ShoppingListDetailsAdapter listAdapter;
	
	private LinearLayout layoutAddItem;
	public static String shoppingId;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_shopping_details);

		setContents();
	}

	private void setContents() {
		layoutAddItem = (LinearLayout) findViewById(R.id.ll_add_item);
		layoutAddItem.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showAddIngredientDialog();
			}
		});
		
		TextView textViewShoppingName = (TextView) findViewById(R.id.tv_shopping_name);
		String name = getIntent().getStringExtra(Const.KEY_SHOPPING_NAME);
		textViewShoppingName.setText(name);

		listviewIngredients = (ListView) findViewById(R.id.lv_shopping_list);
		ingredientList = new ArrayList<ShopIngredient>();
		listAdapter = new ShoppingListDetailsAdapter(ActivityShoppingDetails.this.getParent(), R.layout.list_item_shopping_details, ingredientList){
			@Override
			public void onClick(View v) {
				super.onClick(v);
				showDeleteShoppingDialog(Integer.parseInt(v.getTag().toString()));
			}
			
			@Override
			public boolean onLongClick(View arg0) {
				ShopIngredient sIng = (ShopIngredient) arg0.getTag();
				//TODO
				showEditIngredientDialog(sIng);
				return true;
			}
		};
		listviewIngredients.setAdapter(listAdapter);

		shoppingId = getIntent().getStringExtra(Const.KEY_ID);
		
		getIngredients();
	}

	public void getIngredients() {
		if (ingredientList != null) {
			ingredientList.clear();
		}else{
			ingredientList=new ArrayList<ShopIngredient>();
		}

		Shared.dbAdapter.getIngredients(shoppingId, ingredientList);

		listAdapter.notifyDataSetChanged();
	}

	private void showDeleteShoppingDialog(final int id) {
		AlertDialog dialog = new AlertDialog.Builder(((NavigationActivity)this).getParent()).create();
		dialog.setMessage("Delete Item?");

		dialog.setButton("YES", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				Shared.dbAdapter.deleteIngredient(id);
				getIngredients();
			}
		});

		dialog.setButton2("NO", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		dialog.show();
	}
	
	@SuppressWarnings("deprecation")
	private void showEditIngredientDialog(final ShopIngredient sIng) {
		final Dialog dialog = new Dialog(ActivityShoppingDetails.this.getParent());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_add_ingredients);

		Window window = dialog.getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();

		wlp.gravity = Gravity.CENTER;
		wlp.width = WindowManager.LayoutParams.FILL_PARENT;
		wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);

		dialog.getWindow().setAttributes(wlp);

		dialog.show();

		final EditText edittextQuantity = (EditText) dialog.findViewById(R.id.et_quantity);
		final EditText edittextUnit = (EditText) dialog.findViewById(R.id.et_unit);
		final EditText edittextName = (EditText) dialog.findViewById(R.id.et_name);
		final EditText edittextdescription = (EditText) dialog.findViewById(R.id.et_description);

		edittextQuantity.setText(sIng.getQuantity());
		edittextName.setText(sIng.getName());
		edittextdescription.setText(sIng.getDescription());
		edittextUnit.setText(sIng.getUnit());
		
		
		Button buttonOK = (Button) dialog.findViewById(R.id.btn_ok);
		buttonOK.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final String quantity = edittextQuantity.getText().toString();
				final String unit = edittextUnit.getText().toString();
				final String name = edittextName.getText().toString();
				final String description = edittextdescription.getText().toString();

				boolean isValid = validateAddIngredientForm(quantity, unit, name, description);
				if (isValid) {
					dialog.dismiss();

					Shared.dbAdapter.deleteIngredient(sIng.getID());
					
					sIng.setQuantity(quantity);
					sIng.setUnit(unit);
					sIng.setName(name);
					sIng.setDescription(description);
					
					
					Shared.dbAdapter.addShoppingDetail(sIng);
					
					getIngredients();
				} 
			}
		});
	}
	
	@SuppressWarnings("deprecation")
	private void showAddIngredientDialog() {
		final Dialog dialog = new Dialog(ActivityShoppingDetails.this.getParent());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_add_ingredients);

		Window window = dialog.getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();

		wlp.gravity = Gravity.CENTER;
		wlp.width = WindowManager.LayoutParams.FILL_PARENT;
		wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);

		dialog.getWindow().setAttributes(wlp);

		dialog.show();

		final EditText edittextQuantity = (EditText) dialog.findViewById(R.id.et_quantity);
		final EditText edittextUnit = (EditText) dialog.findViewById(R.id.et_unit);
		final EditText edittextName = (EditText) dialog.findViewById(R.id.et_name);
		final EditText edittextdescription = (EditText) dialog.findViewById(R.id.et_description);

		Button buttonOK = (Button) dialog.findViewById(R.id.btn_ok);
		buttonOK.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final String quantity = edittextQuantity.getText().toString();
				final String unit = edittextUnit.getText().toString();
				final String name = edittextName.getText().toString();
				final String description = edittextdescription.getText().toString();

				boolean isValid = validateAddIngredientForm(quantity, unit, name, description);
				if (isValid) {
					dialog.dismiss();

					ShopIngredient sIng = new ShopIngredient(-1, quantity, unit, name, description, Integer.parseInt(shoppingId));
					Shared.dbAdapter.addShoppingDetail(sIng);
					
					getIngredients();
				} 
			}
		});
	}

	private boolean validateAddIngredientForm(String quantity, String unit, String name, String description) {
		float q = 0f;
		try {
			q = Float.parseFloat(quantity);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (quantity == null || quantity.length() == 0 || q == 0f) {
			Toast.makeText(ActivityShoppingDetails.this.getParent(), "Please input quantity", Toast.LENGTH_SHORT).show();
			return false;
		} else if (unit == null || unit.length() == 0) {
			Toast.makeText(ActivityShoppingDetails.this.getParent(), "Please input unit", Toast.LENGTH_SHORT).show();
			return false;
		} else if (name == null || name.length() == 0) {
			Toast.makeText(ActivityShoppingDetails.this.getParent(), "Please input name", Toast.LENGTH_SHORT).show();
			return false;
		} 
		//		else if (description == null || description.length() == 0) {
		//			Toast.makeText(ActivityCreateNewRecipe.this.getParent(), "Please input description", Toast.LENGTH_SHORT).show();
		//			return false;
		//		} 

		return true;
	}
	
	@Override
	protected void onDestroy() {
		Shared.shopItems.put(shoppingId, listAdapter.getChecked());
		super.onDestroy();
	}
}
